<?php

require_once("./Model/LoginModel.php");
require_once("./Model/User.php");
require_once("./Model/UserTemp.php");

require_once("./View/NavigationView.php");
require_once("./View/LoginView.php");

class LoginController {
	
	 /**
	 * @var LoginView
	 */
	private $loginView;
	
	/**
	 * @var LoginModel
	 */
	private $loginModel;
	
	/**
	 * @var NavigationView
	 */
	private $navView;
	
	/**
	 * @var NavigationView 	$navView
	 * @var LoginModel		$loginModel
	 * @var LoginView		$loginView
	 * @var DALBase			$DALBase
	 */
	public function __construct(NavigationView	$navView,
								LoginView		$loginView,
								LoginModel		$loginModel,
								DALBase			$DALBase) {
		$this->navView = $navView;
		$this->loginView = $loginView;
		$this->loginModel = $loginModel;
		$this->DALBase = $DALBase;
	}
	
	public function handleInput() {
		if ($this->navView->userWantsToLogin() && 
			!$this->loginModel->userIsLoggedIn()) {
				
			$this->loginUser();
		}
		else if ($this->navView->userWantsToLogout() && 
				 $this->loginModel->userIsLoggedIn()) {
				 	
			$this->logoutUser();
		}
	}
	
	/**
	 * @var boolean		$isAuthed
	 * @var UserTemp	$userFromClient
	 * @var User 		$userFromServer
	 * 
	 */
	public function loginUser() {
		if ($this->loginView->isCookiesSet() || $this->loginView->isInputDataSet()) {
			// Currently user is not authed.
			$isAuthed = false;	
			try {
				// Get user from view and user from server.
				$userFromClient = $this->getUser();
				$userFromServer = $this->DALBase->getUser($userFromClient->getUsername());
				
				// If user is found in server do this.
				if ($userFromServer) {
					$isAuthed = $this->checkPassword($userFromClient, $userFromServer);
					
					if ($isAuthed) {
						$this->loginModel->setLoginSessionLoggedIn($userFromServer, $userFromServer->getUsername());
						
						// If user wants to save logindata or logged in using cookies, then renew cookies.
						if ($this->loginView->saveLoginData() || $this->loginView->isCookiesSet()) {
							$userFromServer->createNewTempPassword();
							$this->DALBase->updateUserExpireDate($userFromServer->getId(), $userFromServer->getExpireDate());

							$this->loginView->setLoginDataCookies($userFromServer->getUsername(),
																  $userFromServer->getTempPassword(),
																  $userFromServer->getExpireDate());
						}
					}
					else {
						$this->loginModel->setLoginSessionLoggedOut();
					}
				}
			}
			catch (\Exception $e) {
				// Do nothing.
			}
			
			// Call on view to set messages for login.
			$this->loginView->userAuthed($isAuthed);
		}
	}
	
	/**
	 * @param UserTemp 	$userFromClient
	 * @param User 		$userFromServer
	 * 
	 * @return boolean
	 */
	public function checkPassword(UserTemp $userFromClient, User $userFromServer) {
		if ($this->loginView->isCookiesSet()) {
			return $userFromServer->matchPasswords($userFromClient->getPasswordEncrpyted()) &&
				   $userFromServer->checkExpireDate($userFromClient->getExpireDate());
		}
		else {
			return $userFromServer->matchPasswords($userFromClient->getPasswordEncrpyted());
		}	
	}

	public function logoutUser() {
		$this->loginModel->setLoginSessionLoggedOut();
		$this->loginView->logoutUser();
	}
	
	/**
	 * @return User
	 */
	public function getUser() {
		if ($this->loginView->isCookiesSet()) {
			return new UserTemp($this->loginView->getUsername(), 
								$this->loginView->getPassword(),
								true);
		} else {
			return new UserTemp($this->loginView->getUsername(), 
								$this->loginView->getPassword(),
								false);
		}
	}
}
